﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace QLyHocSinhTHPT
{
    public partial class TTGiangDay : DevComponents.DotNetBar.Office2007Form
    {
        TTGDay tt = new TTGDay();
        public TTGiangDay()
        {
            InitializeComponent();
        }

        public SqlConnection GetConnect()
        {
            return new SqlConnection(@"Data Source=HOANGNGOC-PC\HOANGNGOC;Initial Catalog=QLyHocSinhTHPT;Integrated Security=True");
        }
        private void TTGiangDay_Load(object sender, EventArgs e)
        {
            dgvTTgiangday.DataSource = TTGiangDay_SelectAll();
        }

        private void btnthem_Click(object sender, EventArgs e)
        {
            if (txtmagv.Text.Trim().Equals(" "))
            {
                MessageBox.Show("ban phai nhap ma giao vien");
                txtmagv.Focus();
                return;
            }
            tt.MaGV = txtmagv.Text;
            tt.MaMH = txtmamh.Text;
            tt.Malop = txtmalop.Text;
            tt.ThuDay = txtngayday.Text;
            tt.Tiet = int.Parse(txttietday.Text);

           
                TTGiangDay_Insert(tt);
                dgvTTgiangday.DataSource = TTGiangDay_SelectAll();
           

        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            if (txtmagv.Text.Trim().Equals(" "))
            {
                MessageBox.Show("ban phai nhap ma giao vien");
                txtmagv.Focus();
                return;
            }
            tt.MaGV = txtmagv.Text;
            tt.MaMH = txtmamh.Text;
            tt.Malop = txtmalop.Text;
            tt.ThuDay = txtngayday.Text;
            tt.Tiet = int.Parse(txttietday.Text);

             TTGiangDay_Update(tt);
                dgvTTgiangday.DataSource = TTGiangDay_SelectAll();
           
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            if (txtmagv.Text.Trim().Equals(" "))
            {
                MessageBox.Show("ban phai nhap ma giao vien");
                txtmagv.Focus();
                return;
            }

            TTGDay tt = new TTGDay();
            tt.MaGV = txtmagv.Text;
            TTGiangDay_Delete(tt.MaGV);
            dgvTTgiangday.DataSource = TTGiangDay_SelectAll();
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void txttimkiem_TextChanged(object sender, EventArgs e)
        {
            TTGDay tt = new TTGDay();
            tt.MaGV = txttimkiem.Text;
            dgvTTgiangday.DataSource = TTGiangDay_Search(tt);
          
        }

        private void dgvTTgiangday_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int rowindex = e.RowIndex;
            txtmagv.Text = dgvTTgiangday.Rows[rowindex].Cells[0].Value.ToString();
            txtmamh.Text = dgvTTgiangday.Rows[rowindex].Cells[1].Value.ToString();
            txtmalop.Text = dgvTTgiangday.Rows[rowindex].Cells[2].Value.ToString();
            txtngayday.Text = dgvTTgiangday.Rows[rowindex].Cells[3].Value.ToString();
            txttietday.Text = dgvTTgiangday.Rows[rowindex].Cells[4].Value.ToString();
           

        }

        public DataTable TTGiangDay_SelectAll()
        {
            SqlConnection con = GetConnect();
            con.Open();
            SqlCommand cmd = new SqlCommand("sp_TTGiangDay_GetByAll", con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter adap = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            adap.Fill(dt);
            con.Close();
            return dt;
        }

        public void TTGiangDay_Insert(TTGDay tt)
        {
            SqlConnection con = GetConnect();
            con.Open();
            SqlCommand cmd = new SqlCommand("sp_TTGiangDay_Insert", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@magv", tt.MaGV));
            cmd.Parameters.Add(new SqlParameter("@mamh", tt.MaMH));
            cmd.Parameters.Add(new SqlParameter("@malop", tt.Malop));
            cmd.Parameters.Add(new SqlParameter("@thuday", tt.ThuDay));
            cmd.Parameters.Add(new SqlParameter("@tiet", tt.Tiet));

            cmd.ExecuteNonQuery();
            con.Close();
        }

        public void TTGiangDay_Update(TTGDay tt)
        {
            SqlConnection con = GetConnect();
            con.Open();
            SqlCommand cmd = new SqlCommand("sp_TTGiangDay_Update", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@magv", tt.MaGV));
            cmd.Parameters.Add(new SqlParameter("@mamh", tt.MaMH));
            cmd.Parameters.Add(new SqlParameter("@malop", tt.Malop));
            cmd.Parameters.Add(new SqlParameter("@thuday", tt.ThuDay));
            cmd.Parameters.Add(new SqlParameter("@tiet", tt.Tiet));

            cmd.ExecuteNonQuery();
            con.Close();
        }

        public void TTGiangDay_Delete(string magv)
        {
            SqlConnection con = GetConnect();
            con.Open();
            SqlCommand cmd = new SqlCommand("sp_TTGiangDay_Delete", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@magv", magv));
            cmd.ExecuteNonQuery();
            con.Close();
        }

        public DataTable TTGiangDay_Search(TTGDay tt)
        {

            SqlConnection con = GetConnect();
            con.Open();
            SqlCommand cmd = new SqlCommand("sp_TTGiangDay_Search", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@magv", tt.MaGV));
            SqlDataAdapter adap = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            adap.Fill(dt);
            return dt;
        }
    }
}
